import { getStringByName } from './resource_manager/ResourceManager';
import { systemDateTime } from '@kit.BasicServicesKit';

export enum DateType {
  Later = 0,
  Today = 1,
  Yesterday = 2,
  ThisWeek = 3,
  LastWeek = 4,
  Earlier = 5,
}

export class DateUtil {
  /**
   * 将日期按指定格式输出字符串
   * @param date 待格式化日期
   * @param fmt 指定格式，Y:年 M:月份，d:日，h:12制小时，H:24制小时，m:分，s:秒，q:季度，S:毫秒，E:星期几
   * @returns 日期格式化后字符串
   */
  static formatDate(date: Date, fmt: string): string {
    var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
      "H+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    var week = {
      "0": "日", //"/u65e5",
      "1": "一", //"/u4e00",
      "2": "二", //"/u4e8c",
      "3": "三", //"/u4e09",
      "4": "四", //"/u56db",
      "5": "五", //"/u4e94",
      "6": "六", //"/u516d"
    };
    let reg: RegExp = new RegExp(/(y+)/)
    if (reg.test(fmt)) {
      const result = reg.exec(fmt)
      if (result.length < 1) {
        // 抛出错误
      }
      fmt = fmt.replace(result[0], (date.getFullYear() + "").substring(4 - result[0].length));
    }
    reg = new RegExp(/(E+)/)
    if (reg.test(fmt)) {
      const reg = new RegExp(/(E+)/)
      const result = reg.exec(fmt)
      // fmt = fmt.replace(result[0], ((result[0].length > 1) ? (result[0].length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[date.getDay() + ""]);
      fmt = fmt.replace(result[0], ((result[0].length > 1) ? (result[0].length > 2 ? "星期" : "周") : "") + week[date.getDay() + ""]);
    }
    for (var k in o) {
      const reg = new RegExp("(" + k + ")")
      if (reg.test(fmt)) {
        const result = reg.exec(fmt)
        fmt = fmt.replace(result[0], (result[0].length == 1) ? (o[k]) : (("00" + o[k]).substring(("" + o[k]).length)));
      }
    }
    return fmt;
  }

  // static getDateTypeByNow(date: Date) {
  //   const target = dayjs(date);
  //   const today = dayjs().startOf('day'), lastDay = today.subtract(1, 'day'),
  //     thisWeek = today.day(1), lastWeek = thisWeek.subtract(1, 'week');
  //   return target.isAfter(today) ? DateType.Today
  //     : target.isAfter(lastDay) ? DateType.Yesterday
  //     : target.isAfter(thisWeek) ? DateType.ThisWeek
  //     : target.isAfter(lastWeek) ? DateType.LastWeek
  //     : DateType.Earlier;
  // }

  /**
   * 邮件列表的邮件显示
   * 当天邮件时间 显示 HH:mm
   * 非当前显示 yyyy-MM-dd
   * @param date
   * @returns
   */
  static getDateString(date: Date) {
    let nowDate = new Date()
    nowDate.setHours(23, 59, 59)
    const nowTime = nowDate.getTime()
    const targetTime = date.getTime()
    if (nowTime - targetTime < 60 * 60 * 24 * 1000) { // 判断为今天
      return DateUtil.formatDate(date, "HH:mm")
    } else {
      return DateUtil.formatDate(date, "yyyy/MM/dd")
    }
  }

  static dateStringWithFormatHHmm(date: Date, seperator: string = ""): string {
    let h = date.getHours();
    let hStr = h < 10 ? '0' + h : h;
    let m = date.getMinutes();
    let mStr = m < 10 ? '0' + m : m;
    let s = date.getSeconds();
    let sStr = s < 10 ? '0' + s : s;
    return `${hStr}${seperator}${mStr}${seperator}${sStr}`;
  }

  static titleStringMap: Map<DateType, string> = new Map([
    [DateType.Later, "later"],
    [DateType.Today, "today"],
    [DateType.Yesterday, "yesterday"],
    [DateType.ThisWeek, "this_week"],
    [DateType.LastWeek, "last_week"],
    [DateType.Earlier, "before"],
  ]);

  static titleForDatetype(dateType: DateType): string {
    return getStringByName(DateUtil.titleStringMap.get(dateType) || "later")
  }

  /**
   * 获取当前系统时间戳
   * @returns
   */
  static async getSystemCurrentTimes() {
    return await systemDateTime.getCurrentTime(false)
  }
}